﻿package org.openChart.display
{
	import flash.display.BlendMode;
	import flash.display.LineScaleMode;
	import flash.geom.Point;
	import flash.media.SoundLoaderContext;
	import org.openChart.meta.BaseObject;
	import org.openChart.sprite.Color;
	import org.openChart.util.Rectangle;
	
	/**
	 * The group of color labels.
	 * 
	 * @author Chognyuan
	 */
	public class DisplayColorLabelGroup extends DisplayBaseObject implements DisplayInterface
	{
		// The layout of color label group.
		private var layout_:String = TOP_HORIZONTAL;
		// The array of color labels.
		private var labels_:Array = new Array();
		// Whether show the rectangle area of color labels.
		private var showRectangle_:Boolean = false;
		// The parent.
		private var parent_:DisplayScreen;
		// The count of color labels.
		private var length_:uint = 0;
		// The maximum width of group.
		private var maxWidth_:Number = Number.MAX_VALUE;
		// The maximum height of gorup.
		private var maxHeight_:Number = Number.MAX_VALUE;
		// The width of group.
		private var width_:Number = 0;
		// The height of group.
		private var height_:Number = 0;
		// The backgroud color of group.
		private var bgColor_:uint;
		// The thickness of the edage of group area.
		private var thickenss_:uint = 1;
		
		public static const TOP_HORIZONTAL:String = "top-horizontal";
		public static const BOTTOM_HORIZONTAL:String = "bottom-horizontal";
		public static const LEFT_VERTICAL:String = "left-horizontal";
		public static const RIGHT_VERTICAL:String = "right-horizontal";
		
		
		public function DisplayColorLabelGroup(object:BaseObject, parent:DisplayScreen) 
		{
			parent_ = parent;
			labels_ = new Array();
			super(object);
		}
		
		/**
		 * Initialize the color label group.
		 */
		public override function initialize():void
		{
			initSize();
		}	
		
		/**
		 * Initialize the size of color label group.
		 */
		public function initSize():void
		{
			var groupWidth:Number = 0;
			var groupHeight:Number = 0;
			for (var i:uint = 0; i < length; i++ )
			{
				var colorLabel:DisplayColorLabel = getAt(i);
				// If the color label group is horizontal, there may be more than one row.
				if (layout == TOP_HORIZONTAL || layout == BOTTOM_HORIZONTAL)
				{
					if (i == 1)
					{
						groupHeight = colorLabel.height;
					}
					
					if (groupWidth < maxWidth_ - colorLabel.width)
					{
						groupWidth += colorLabel.width;
					}
					else
					{
						groupWidth = 0;
						width_ = maxWidth_;
						groupHeight += colorLabel.height;
					}
				}
				else if (layout == LEFT_VERTICAL || layout == RIGHT_VERTICAL)
				{
					groupWidth = Math.max(groupWidth, colorLabel.width);
					groupHeight += colorLabel.height;
				}
			}
			
			if (width_ < groupWidth)
			{
				width_ = groupWidth;
			}
			height_ = groupHeight;
		}
		
		/**
		 * Set the position of color labels group.
		 * And we can set proistion of each color label here.
		 * @param	rectangle
		 */
		public override function resize(rectangle:Rectangle):void
		{
			
			if (this.layout == DisplayColorLabelGroup.BOTTOM_HORIZONTAL)
			{
				rectangle.bottom = rectangle.bottom - height_;
				this.position.x = rectangle.left + 50;
				this.position.y = rectangle.bottom;
			}
			else if (this.layout == DisplayColorLabelGroup.TOP_HORIZONTAL)
			{
				
				this.position.x = rectangle.left + 50;
				this.position.y = rectangle.top;
				rectangle.top += height_;
			}
			else if (this.layout == DisplayColorLabelGroup.LEFT_VERTICAL)
			{
				this.position.y = rectangle.top ;
				this.position.x = rectangle.left;
				rectangle.left = maxWidth_;
			}
			else if (this.layout == DisplayColorLabelGroup.RIGHT_VERTICAL)
			{
				rectangle.right = rectangle.right - this.maxWidth_;
				this.position.y = rectangle.top;
				this.position.x = rectangle.right;
			}
			resizePostions();
		}
		
		/**
		 * Set the positions of each color label.
		 */
		public function resizePostions():void
		{
			var groupWidth:Number = 0;
			var groupHeight:Number = 0;
			for (var i:uint = 0; i < length; i++ )
			{
				var colorLabel:DisplayColorLabel = getAt(i);
				
				// If the color label group is horizontal, there may be more than one row.
				if (layout == TOP_HORIZONTAL || layout == BOTTOM_HORIZONTAL)
				{	
					if (groupWidth < maxWidth_ - colorLabel.width)
					{
						colorLabel.position.x = position.x + groupWidth;
						colorLabel.position.y = position.y + groupHeight;
						groupWidth += colorLabel.width;
					}
					else
					{
						groupWidth = 0;
						groupHeight += colorLabel.height;
						colorLabel.position.x = position.x + groupWidth;
						colorLabel.position.y = position.y + groupHeight;
						
					}
					
				}
				else if (layout == LEFT_VERTICAL || layout == RIGHT_VERTICAL)
				{
					colorLabel.position.x = position.x + groupWidth;
					colorLabel.position.y = position.y + groupHeight;
					groupHeight += colorLabel.height;
				}
			}
		}
		
		/**
		 * Get color label at position index.
		 * @param	index
		 * @return
		 */
		public function getAt(index:uint):DisplayColorLabel
		{
			if (index < 0 || index >= length_)
				throw new Error("Index out of boundary error!");
			return labels_[index] as DisplayColorLabel;
		}
		
		/**
		 * Remove all the color labels.
		 */
		public function clear():void
		{
			for (var i:uint = 0; i < length; i++ )
			{
				labels_.pop();
			}
			
			length_ = 0;
			width_ = 0;
			height_ = 0;
		}
		
		/**
		 * Adding a color to this group.
		 * @param	colorLabel
		 */
		public function addColorLabel( colorLabel:DisplayColorLabel ):void
		{
			colorLabel.maxWidth = maxWidth_;
			colorLabel.initSize();
			this.labels_.push(colorLabel);
			length_++;
		}
		
		public function set layout( layout_:String ):void
		{
			this.layout_ = layout_;
		}
		
		public function get layout():String
		{
			return this.layout_;
		}
		
		public function set showRectangle( showRectangle_:Boolean ):void
		{
			this.showRectangle_= showRectangle;
		}
		
		public function get showRectangle():Boolean
		{
			return this.showRectangle_;
		}
		
		public function set bgColor(bgColor_:uint):void
		{
			this.bgColor_ = bgColor_;
		}
		
		public function get bgColor():uint
		{
			return bgColor_;
		}
		
		public function set thickenss(thickenss_:Number):void
		{
			this.thickenss_ = thickenss_;
		}
		
		public function get thickenss():Number
		{
			return thickenss_;
		}
		
		// read-only properties
		public function get length():uint
		{
			return length_;
		}
		
		public function get width():Number
		{
			return width_;
		}
		
		public function get height():Number
		{
			return height_;
		}
		
		// write-only properties
		public function set maxWidth(maxWidth_:Number):void
		{
			this.maxWidth_ = maxWidth_;
		}
		
		public function set maxHeight(maxHeight_:Number):void
		{
			this.maxHeight_ = maxHeight_;
		}
		
		public function toString():String
		{
			var str:String = "\nObject:DisplayColorLabelGroup ";
			str += "[layout =" + layout + "]";
			str += "[showRectangle =" + showRectangle + "]";
			str += "[length =" + length + "]";
			str += "[maxWidth =" + maxWidth_ + "]";
			str += "[maxHeight =" + maxHeight_ + "]";
			str += "[width =" + width + "]";
			str += "[height =" + height + "]";
			str += "[bgcolor =" + bgColor + "]";
			str += "[thickness =" + thickenss + "]";
			str += "[position: x = " + position.x +", y = " + position.y + "]";
			str += "[labels = ";
			for (var i:uint = 0; i < length; i++ )
			{
				str += getAt(i);
			}
			str += "]";
			return str;
		}
	}
}